﻿using System.Collections.Specialized;
using System.Text;

namespace Yz.Core.Extensions
{
    public static class HttpExtension
    {

        #region Url操作
        /// <summary>
        ///     根据给定的键值对，创建URL查询字符串。
        /// </summary>
        /// <param name="queryString">包含URL查询参数的键值对列表。</param>
        /// <returns>URL查询字符串。</returns>
        public static string ToEncodeQueryString(this NameValueCollection queryString)
        {
            return queryString.ToEncodeQueryString(true);
        }

        /// <summary>
        ///     根据给定的键值对，创建URL查询字符串。
        /// </summary>
        /// <param name="queryString">包含URL查询参数的键值对列表。</param>
        /// <param name="encodeParameters">如果查询参数需要编码，则返回<c>true</c>; 否则返回<c>false</c>。</param>
        /// <returns>URL查询字符串。</returns>
        public static string ToEncodeQueryString(this NameValueCollection queryString, bool encodeParameters)
        {
            var builder = new StringBuilder();
            string str = null;
            foreach (string str2 in queryString.Keys)
            {
                if (!string.IsNullOrEmpty(str)) builder.Append(str);
                var queryValue = queryString[str2];
                if (encodeParameters) queryValue = queryValue.ToUrlEncode();
                builder.AppendFormat("{0}={1}", str2, queryValue);
                str = "&";
            }

            return builder.ToString();
        }

        /// <summary>
        ///     根据给定的基URL和参数列表创建URL字符串。
        /// </summary>
        /// <param name="baseURL">基URL。</param>
        /// <param name="queryString">包含URL查询参数的键值对列表。</param>
        /// <returns>URL</returns>
        public static string ToEncodeUrl(this string baseURL, NameValueCollection queryString)
        {
            if (queryString == null || queryString.Count == 0) return baseURL;
            string str = null;
            if (baseURL.Contains("?"))
                str = "&";
            else
                str = "?";
            return string.Format("{0}{1}{2}", baseURL, str, queryString.ToEncodeQueryString());
        }
        /// <summary>
        ///     解析URL中的查询字符串。
        /// </summary>
        /// <param name="url">需要解析的URL</param>
        /// <returns>查询字符串中的参数键值对列表。</returns>
        public static NameValueCollection ToParseQueryString(this string url)
        {
            return url.ToParseQueryString(true);
        }

        /// <summary>
        ///     解析URL中的查询字符串。
        /// </summary>
        /// <param name="url">需要解析的URL</param>
        /// <param name="decodeParameters">如果URL查询字符串中的参数需要URL解码，则返回<c>true</c>；否则返回<c>false</c>。</param>
        /// <returns>查询字符串中的参数键值对列表。</returns>
        public static NameValueCollection ToParseQueryString(this string url, bool decodeParameters)
        {
            var values = new NameValueCollection();
            var strArray = url.Split('?');
            if (strArray.Length > 2) throw new Exception("URL格式无效。");
            var qs = "";
            if (strArray.Length == 2)
                qs = strArray[1];
            else
                qs = strArray[0];

            foreach (var str in qs.Split('&'))
            {
                var strArray2 = str.Split('=');
                if (strArray2.Length != 2) throw new Exception("URL查询字符串的格式无效。");
                var queryValue = strArray2[1];
                var str2 = strArray2[0];
                if (decodeParameters)
                {
                    str2 = str2.ToUrlDecode();
                    queryValue = queryValue.ToUrlDecode();
                }

                values.Add(str2, queryValue);
            }

            return values;
        }

        #endregion

        /// <summary>
        ///     检查给定路径是否是URL地址。
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool ValidFullUrl(this string path)
        {
            if (string.IsNullOrEmpty(path))
                return false;
            path = path.ToLower();
            return path.IndexOf("http:") == 0 || path.IndexOf("https:") == 0;
        }
    }
}
